package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.SysMenu;
import com.contest.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.contest.entity.WebSiteSetting;
import com.contest.service.WebSiteSettingService;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;


/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-01-23 14:15:49
 */

@Controller
@RequestMapping("/webSiteSetting")
public class WebSiteSettingController {

    @Autowired
    WebSiteSettingService webSiteSettingService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String, Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<WebSiteSetting> mPage = new Page<WebSiteSetting>(page, rows);

        Wrapper<WebSiteSetting> w = new EntityWrapper<>();
        String o = String.valueOf(param.get("name"));
        if(o != null && !o.equals("") && !o.equals("null")) {
            String name = String.valueOf(o);
            w.like("fd_title",name);
        }

        mPage = webSiteSettingService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(), mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(HttpServletRequest request, @RequestParam("bg") CommonsMultipartFile bg, WebSiteSetting webSiteSetting) {
        if (bg != null && !"".equals(bg.getOriginalFilename())) {
            webSiteSetting.setFdBgPic(FileUtil.uploadFile(request, bg));
        }
        webSiteSetting.setFdId(idWorker.nextId() + "");
        webSiteSetting.setFdCreatedTime(DateUtil.now());
        if (webSiteSettingService.insert(webSiteSetting)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        WebSiteSetting webSiteSetting = webSiteSettingService.selectById(fdId);
        model.addAttribute("webSiteSetting", webSiteSetting);
        return "webSiteSetting/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (webSiteSettingService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(HttpServletRequest request, @RequestParam("bg") CommonsMultipartFile bg, WebSiteSetting webSiteSetting) {
        if (bg != null && !"".equals(bg.getOriginalFilename())) {
            webSiteSetting.setFdBgPic(FileUtil.uploadFile(request, bg));
        }
        if (webSiteSettingService.updateById(webSiteSetting)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

    @PostMapping("/client/listWebSiteSetting")
    @ResponseBody
    public R listWebSiteSetting(String type){
        EntityWrapper<WebSiteSetting> siteSettingEntityWrapper = new EntityWrapper<WebSiteSetting>();
        siteSettingEntityWrapper.addFilter("fd_is_enable={0} and fd_type={1}",1,type);
        siteSettingEntityWrapper.orderBy("fd_created_time",false);
        return R.ok(webSiteSettingService.selectList(siteSettingEntityWrapper));
    }

}
